import request from '@/config/axios'

// ERP 销售订单 VO
export interface SaleOrderVO {
  id: number // 订单工单编号
  no: string // 销售订单号
  customerId: number // 客户编号
  orderTime: Date // 订单时间
  totalCount: number // 合计数量
  totalPrice: number // 合计金额，单位：元
  completeDate:Date
  status: number // 状态
  remark: string // 备注
  outCount: number // 销售出库数量
  returnCount: number // 销售退货数量
}

// ERP 销售订单 API
export const SaleOrderApi = {
  // 查询销售订单分页
  getSaleOrderPage: async (params: any) => {
    return await request.get({ url: `/erp/sale-order/page`, params })
  },

  //获得精友车型信息
  getJyCarTypeByVin: async (vin:string) => {
    return await request.get({ url: `/erp/sale-order/getJyCarTypeByVin/`+vin })
  },

  //获取采购单
  getPurchaseBySaleOrderId: async (saleOrderId) => {
    return await request.get({ url: `/erp/sale-order/getPurchaseBySaleOrderId/`+saleOrderId })
  },

  //获取销售出库单
  querySaleOutById: async (saleOrderId) => {
    return await request.get({ url: `/erp/sale-order/querySaleOutById/`+saleOrderId })
  },

  //获得精友零件名称精确检索
  getStdPartNameByAmVehicleId: async (amVehicleId:string, partName:string,vin:string) => {
    return await request.get({ url: `/erp/sale-order/getStdPartNameByAmVehicleId/` , params: {
        amVehicleId,
        partName,
        vin
      }})
  },

  // 查询销售订单详情
  getSaleOrder: async (id: number) => {
    return await request.get({ url: `/erp/sale-order/get?id=` + id })
  },

  getInventory: async (id: number) => {
    return await request.get({ url: `/erp/sale-order/getInventory?id=` + id })
  },

  // 新增销售订单
  createSaleOrder: async (data: SaleOrderVO) => {
    return await request.post({ url: `/erp/sale-order/create`, data })
  },

  // 修改销售订单
  updateSaleOrder: async (data: SaleOrderVO) => {
    return await request.put({ url: `/erp/sale-order/update`, data })
  },

  updateRemark: async (data) => {
    return await request.put({ url: `/erp/sale-order/updateRemark`, data })
  },

  //同步销售单列表
  syncEngineerList: async (data) => {
    return await request.put({ url: `/erp/sale-order/syncEngineerList`, data })
  },

  //同步采购价格
  syncPurchasePrice: async (data) => {
    return await request.put({ url: `/erp/sale-order/syncPurchasePrice`, data })
  },

  /**
   * 查询采购订单
   * @param id
   */
  queryPurchaseOrderById: async (id) => {
    return await request.get({ url: `/erp/sale-order/queryPurchaseOrderById/`+id })
  },

  /**
   * 查询采购入库单
   * @param id
   */
  queryPurchaseInById: async (id) => {
    return await request.get({ url: `/erp/sale-order/queryPurchaseInById/`+id })
  },

  /**
   * 查询付款单
   * @param id
   */
  queryFinancePaymentById: async (id) => {
    return await request.get({ url: `/erp/sale-order/queryFinancePaymentById/`+id })
  },

  /**
   * 查看收款
   * @param id
   */
  queryFinanceReceiptById: async (id) => {
    return await request.get({ url: `/erp/sale-order/queryFinanceReceiptById/`+id })
  },

  // 更新销售订单的完成时间
  updateCompleteDate: async (data: SaleOrderVO) => {
    return await request.put({ url: `/erp/sale-order/updateCompleteDate`, data })
  },

  // 更新销售订单的状态
  updateSaleOrderStatus: async (id: number, status: number) => {
    return await request.put({
      url: `/erp/sale-order/update-status`,
      params: {
        id,
        status
      }
    })
  },
  //根据销售单生成采购单
  createPurchaseOrderBySaleOrder: async (id: number) => {
    return await request.put({
      url: `/erp/sale-order/createPurchaseOrderBySaleOrder`,
      params: {
        id
      }
    })
  },


  //同步完工状态
  updateCompleteStatus: async (id: number, completeStatus: number) => {
    return await request.put({
      url: `/erp/sale-order/updateCompleteStatus`,
      params: {
        id,
        completeStatus
      }
    })
  },

  // 删除销售订单
  deleteSaleOrder: async (ids: number[]) => {
    return await request.delete({
      url: `/erp/sale-order/delete`,
      params: {
        ids: ids.join(',')
      }
    })
  },

  // 导出销售订单 Excel
  exportSaleOrder: async (params: any) => {
    return await request.download({ url: `/erp/sale-order/export-excel`, params })
  }
}
